<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/5/7
     * Time: 下午5:24
     */

    namespace plugins\TencentSms\controller;


    use app\common\controller\Common;

    use plugins\TencentSms\utils\UtilSms;
    use think\Db;
    use think\facade\Log;


    class TencentSms extends Common
    {

        /**
         * @param int    $phoneNum 手机
         * @param string $content  内容
         *
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function sendMessage($phoneNum,$content = '')
        {

            if ($content)
            {
                return $this->message($phoneNum,$content);
            }
        }

        /**
         * @param int $phoneCode 验证码信息
         * @param int $phoneNum  手机号码
         *
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function VerifiCode($phoneNum,$phoneCode)
        {

            if (!is_numeric($phoneCode))
            {
                $this->error('Code只能是数字!');
            }
            return $this->message($phoneNum,"欢迎使用，您的验证码为' . $phoneCode . '，请妥善保存，如非本人操作，请忽略本短信。");
        }

        /**
         * @param        $phoneNum
         * @param string $message
         *
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function message($phoneNum,$message = '')
        {

            // 读取微信插件配置
            $Config = plugin_config('TencentSms');

            // 插件启用状态检测
            $Status = $this->getPluginStatus();
            if (!$Status)
                $this->error('腾讯云短信插件未开启或未安装!');
            // 检测插件必填项
            if (!isset($Config['Appid']) || $Config['Appid'] == '' || !isset($Config['Appkey']) || $Config['Appkey'] == '')
            {
                $this->error('短信配置不完整!');
            }

            if (!empty($message))
            {

                $api     = new UtilSms($Config['Appid'],$Config['Appkey']);
                $content = $api->send(0,'86',$phoneNum,$message,'','');

                $json = json_decode($content,true);

                if (isset($json['ActionStatus']) && $json['ActionStatus'] == 'FAIL')
                {
                    Log::alert($json);
                    return ['code' => 500,'msg' => $json['ErrorInfo']];
                }
                else if ($json['errmsg'] == 'OK' && $json['result'] == 0)
                {
                    Log::alert($json);

                    return ['code' => 200,'msg' => '发送成功!',$json];
                }
                else
                {
                    return ['code' => 500];

                }
            }
            else
            {
                return ['code' => 500,'msg' => '请填写手机号码!'];

            }
        }

        /**
         * 检测插件是否安装和开启
         *
         * @return bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        private function getPluginStatus()
        {

            $pluginRecord = Db::name('admin_plugin')->where(['name' => 'TencentSms'])->find();
            if (is_null($pluginRecord))
            {
                return false;
            }
            else
            {
                // 检测插件是否开启
                $Status = $pluginRecord['status'];
                return $Status != 1 ? false : true;
            }
        }
    }